import { actions } from "mirrorx";
// 引入services，如不需要接口请求可不写
import * as api from "./service";
// 接口返回数据公共处理方法，根据具体需要
import {Message} from "tinper-bee";
import { processData, isEmptyObj,processData2 } from "utils";
import moment from 'moment';

/**
 *          btnFlag为按钮状态，新增、修改是可编辑，查看详情不可编辑，
 *          新增表格为空
 *          修改需要将行数据带上并显示在卡片页面
 *          查看详情携带行数据但是表格不可编辑
 *          0表示新增、1表示编辑，2表示查看详情 3提交
 *async loadList(param, getState) {
 *          rowData为行数据
*/


export default {
    // 确定 Store 中的数据模型作用域
    name: "PromoterSalary",
    // 设置当前 Model 所需的初始化 state
    initialState: {
        rowData: {},
        showLoading: false,
        list: [],
        selectDataList: {},
        personData: {},
        authCodeList:[],
        newRowData: {},
        oldRowData: {},
        aliaCustomerList: [],
        orderTypes: [],
        childList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPages: 1,
        total: 0,
        detail: {},
        searchParam: {},
        queryParam: {
            pageParams: {
                pageIndex: 0,
                pageSize: 10,
                searchExtend: {"search_AUTH_APPCODE": "PromoterSalary" }
            },
            // groupParams: [],
            whereParams: {
            }
        },
        validateNum: 99,//不存在的step

    },
    reducers: {
        /**
         * 纯函数，相当于 Redux 中的 Reducer，只负责对数据的更新。
         * @param {*} state
         * @param {*} data
         */
        updateState(state, data) { //更新state
            return {
                ...state,
                ...data
            };
        }
    },
    effects: {
        /**
         * 加载列表数据
         * @param {*} param
         * @param {*} getState
         */
        async loadList(param, getState) {
            // 正在加载数据，显示加载 Loading 图标
            actions.PromoterSalary.updateState({ showLoading: true });
            // 调用 getList 请求数据
            let _param = param || getState().PromoterSalary.queryParam;
            //_param["search_NULL_applicant"] = true;
            let res = processData2(await api.getList(param));
            //let {data:res}=result;
            let _state = {
                showLoading: false,
                queryParam: _param //更新搜索条件
            }
            if (res) {
                _state = Object.assign({}, _state, {
                    list: res.content,
                    pageIndex: res.number + 1,
                    totalPages: res.totalPages,
                    total: res.totalElements,
                    pageSize: res.size,
                })
            }
            actions.PromoterSalary.updateState(_state);


            // 正在加载数据，显示加载 Loading 图标
            // actions.PromoterSalary.updateState({ showLoading:true });
            // if(!param) param = {};
            //     param.pageIndex = param.pageIndex ? param.pageIndex - 1 : 0;
            //     param.pageSize = param.pageSize ? param.pageSize : 10;
            //     param.auditStatus = param.auditStatus || param.auditStatus == "" ? param.auditStatus : 'N';

            // let res = processData2(await api.getList(param));
            // actions.PromoterSalary.updateState({  showLoading:false })
            // if (res) {
            //     if(res.content&&res.content.length){
            //         for(let i=0;i<res.content.length;i++){
            //             let temp = Object.assign({},res.content[i]);
            //             res.content[i].key=i+1;
            //         }
            //     }
            //     actions.PromoterSalary.updateState({
            //         list: res.content,
            //         pageIndex:res.number + 1,
            //         totalPages:res.totalPages,
            //         total:res.totalElements
            //     });
            // }
        },
        
        async getAuth(param, getState) {//保存
            let res = await api.getAuth();
            if(res.data.msg) {
                res = [];
            }else {
                res = res.data;
            }
            
            sessionStorage.setItem("PromoterSalary",JSON.stringify(res || []));
        },

        //同步NCC
        async syncNCC(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            processData2(await api.syncNCC(param), '操作成功');


            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },
        
        async getSelectData(param, getState) {
            // 正在加载数据，显示加载 Loading 图标
            actions.PromoterSalary.updateState({ showLoading: true })
            //调用 getList 请求数据
            let res = await api.getSelectData(param);

            if (res.data) {
                actions.PromoterSalary.updateState({
                    selectDataList: res.data,
                    showLoading: false
                });
            }
        },

        async getTaxpayerNumber(param, getState, callback) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.getTaxpayerNumber(param.param));
            if (res && isEmptyObj(res)) {
                param.callback(res);
            }
            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },

        async getChildTableData(param, getState, callback) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.getChildTableData(param.param));
            res && param.callback && param.callback(res.data);
            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },

        async createCustomer(param, getState) {
            // 正在加载数据，显示加载 Loading 图标
            actions.PromoterSalary.updateState({ showLoading: true })
            //调用 getList 请求数据
            let res = processData2(await api.createCustomer(param), "操作成功");

            if (res && res.code == 1) {
                actions.PromoterSalary.loadList(getState().PromoterSalary.queryParam);
            }
            actions.PromoterSalary.updateState({
                showLoading: false
            });
        },

        async loadChildList(param, getState) {
            // 正在加载数据，显示加载 Loading 图标
            actions.PromoterSalary.updateState({ showLoading: true })
            if (param) {
                param.pageIndex = param.pageIndex ? param.pageIndex - 1 : 0;
                param.pageSize = param.pageSize ? param.pageSize : 10;
            } else {
                param = {}
            }
            //调用 getList 请求数据
            let res = processData2(await api.getChildList(param));
            actions.PromoterSalary.updateState({ list: res.content, showLoading: false });
        },

        async updateToNCC({ param, callback }, getState) {//保存
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.updateToNCC(param), '保存成功');

            if (res) {
                window.history.go(-1);
            }
            actions.PromoterSalary.updateState({
                showLoading: false,
            });
        },

        /**
         * getSelect：获取下拉列表数据
         * @param {*} param
         * @param {*} getState
         */
        getOrderTypes(param, getState) {
            actions.PromoterSalary.updateState({
                orderTypes: [{
                    "code": "0",
                    "name": "D001"
                }, {
                    "code": "1",
                    "name": "D002"
                }, {
                    "code": "2",
                    "name": "D003"
                }, {
                    "code": "3",
                    "name": "D004"
                }]
            })
        },

        /**
         * getSelect：保存table数据
         * @param {*} param
         * @param {*} getState
         */
        async saveList(param, getState) {
            let result = await api.saveList(param);
            return result;
        },
        /**
         * 删除table数据
         * @param {*} id
         * @param {*} getState
         */
        async removeList(param, getState) {
            let result = processData2(await api.deleteList(param), '操作成功');
            return result;
        },

        async OnResampling(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            processData2(await api.OnResampling(param), '重新抽样成功');

            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },

        async VerificationCargo(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            processData2(await api.VerificationCargo(param), '操作成功');


            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },


        async submitBill(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.submitBill(param), '操作成功');
            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },


        async save({ param, callback }, getState) {//保存
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.save(param), '保存成功');

            if (res) {
                window.history.go(-1);
            }
            actions.PromoterSalary.updateState({
                showLoading: false,
            });
        },

        async submitFile(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.submitFile(param.file), "操作成功");
            actions.PromoterSalary.updateState({
                showLoading: false
            })
            if (res) {
                actions.PromoterSalary.queryDetail(param);
            }
        },

        async submitFileDelete(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.submitFileDelete(param.file), "操作成功");
            actions.PromoterSalary.updateState({
                showLoading: false
            })
            if (res) {
                actions.PromoterSalary.queryDetail(param);
            }
        },

        async delItem(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            processData2(await api.del(param), '删除成功');
            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },

        async confirmCancelPumping(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.confirmCancelPumping(param), '操作成功');
        },

        async confirmVerify(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.confirmVerify(param), '操作成功');
        },

        async getPerson(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.getPerson(param));
            if (res) {
                actions.PromoterSalary.updateState({
                    personData: res,
                    showLoading: false
                })
            }
        },

        async humanVerify(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData2(await api.humanVerify(param), '操作成功');
        },

        async queryDetail(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true,

            });
            let res = processData2(await api.getDetail(param.data));
            if (res) {
                actions.PromoterSalary.updateState({
                    showLoading: false,
                    rowData: res,
                    newRowData: res,
                    oldRowData: res

                });
                param.callback && param.callback(res);
            }
        },

        async printExcel(param) {
            let res = processData2(await api.queryPrintTemplateAllocate(param.queryParams), '');
            if (!res || !res.res_code) return false;
            await api.printExcel({
                tenantId: 'tenant',
                printcode: res.res_code,
                serverUrl: `${GROBAL_HTTP_OCCBASE}/jx_PromoterSalary/dataForPrint`,
                params: encodeURIComponent(JSON.stringify(param.printParams)),
                sendType: 'post'
            })
        },

        async exportExcel(param) {
            let res = await api.exportExcel(param || {});
            if(res.data && res.data.msg) {
                Message.create({ content: res.data.msg, color: 'warning', duration: 10 });
                return;
            }
        },
        /**
         * 审批驳回
         * @param {*} id
         * @param {*} getState
         */
        async EnableBill(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            processData2(await api.enableBill(param), '操作成功');
            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },


        async getAliaCustomer(param) {
            let res = processData2(await api.getAliaCustomer(param));
            if (res) {
                actions.PromoterSalary.updateState({
                    aliaCustomerList: res
                })
            }

        },

        /**
         * 审批驳回
         * @param {*} id
         * @param {*} getState
         */
        async ButtonSetsModel(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            let res = processData(await api.ButtonSetsService(param), '操作成功');

            actions.PromoterSalary.updateState({
                showLoading: false
            })

            actions.PromoterSalary.loadList(getState().PromoterSalary.queryParam);
        },
        /**
         * 审批驳回
         * @param {*} id
         * @param {*} getState
         */
        async DisableBill(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            processData2(await api.disableBill(param), '操作成功');
            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },

        async SubmitItem(param, getState) {
            actions.PromoterSalary.updateState({
                showLoading: true
            })
            processData2(await api.submitItem(param), '操作成功');
            actions.PromoterSalary.updateState({
                showLoading: false
            })
        },


    }
};